package com.xhwl.centiotdata.suppliersystem.baisi;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.exception.ApplicationException;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: yanyixiong
 * @Description: 请求接入,认证鉴权等
 * @Date: create in 16:21 2022/1/4
 */
@Slf4j
@Service
public class GeneralBaiSiHandler {

    public JSONObject post(String path, Map<String,Object> params){
        log.info("【BaiSi post】path:{};params:{}", path, JSON.toJSONString(params));
        String result = null;
        try {
            result = HttpRequest
                    .post(path)
                    .body(JSON.toJSONString(params))
                    .execute()
                    .body();
        } catch (HttpException e) {
            log.error("【BaiSi post】path:{};params:{}; request failed!", path, JSON.toJSONString(params));
            e.printStackTrace();
            throw new ApplicationException();
        }
        if (StringUtils.isEmpty(result)) {
            log.error("【BaiSi post】path:{};params:{};result is null!", path, JSON.toJSONString(params));
            throw new BusinessException();
        }
        log.info("【BaiSi post】path:{};params:{};result:{}", path, JSON.toJSONString(params), result);
        return JSON.parseObject(result);
    }

}
